﻿using GameFramework;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce
{
    public class AndroidCallBackInfo : GameFrameworkComponent
    {
        public string LaunchInfo = string.Empty;

//        void Start()
//        {
//            GetInfo();
//        }

        /// <summary>
        /// 接受安卓回调信息
        /// </summary>
        /// <param name="launchInfo"></param>
        public void OnLaunchInfo(string launchInfo)
        {
            LaunchInfo = launchInfo;
            Log.Info("安卓传来的信息：{0}", launchInfo);
            if (!string.IsNullOrEmpty(LaunchInfo))
            {
                GameEntry.Event.Fire(this, ReferencePool.Acquire<LaunchEventArgs>().Fill());
            }
        }

        public void GetInfo()
        {
#if UNITY_ANDROID&& !UNITY_EDITOR
            AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            jo.Call("getLaunchInfo");
#endif
        }

        private void OnApplicationPause(bool focus)
        {
            //进入程序状态更改为前台
            if (focus)
            {
                Log.Info("程序进入后台");
            }
            else
            {
                Log.Info("程序进入前台");

                try
                {
                    if (GameEntry.Http.Sign != string.Empty)
                    {
//                        GameEntry.Http.Form(null, "findRpcFuDouInfo");
                        GameEntry.Http.Form(null, "showUserGetSwingInfo");
                        GameEntry.Http.Form(null, "showUserWishInfo");
                        GameEntry.Http.Get(null, "selectUserInfo");
                    }
                }
                catch
                {
                    // ignored
                }
            }

            GetInfo();
        }
    }
}